/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 * Description: mc2 dfx
 * Author: huawei
 * Create: 2024-05-24
 */

#ifndef ASCEND_ACE_COMOP_HCCL_HCCL_AI_CPU_KERNEL_DFX_DFX_EXTEND_INFO_H_
#define ASCEND_ACE_COMOP_HCCL_HCCL_AI_CPU_KERNEL_DFX_DFX_EXTEND_INFO_H_
#include <cstdint>
#include <string>
#include <sstream>
#include <deque>

namespace dfx {
enum class KfcStatus : int64_t {
    kDefault = 0,
    kOneStart,
    kOneFinished,
    kTimeOut,
};
enum class CqeStatus : int64_t {
    kDefault = 0,
    kCqeException,
    kCqeTimeOut,
    kCqeInnerError,
    kCqeUnknown,
};
enum class PollStatus : int64_t {
    kDefault = 0,
    kStopAsException,
};
enum class CommandToKfc : int64_t {
    kDefault = 0,
    kClear,
    kStop,
    kRestart
};
enum class CommandToBackGroud : int64_t {
    kDefault = 0,
    kStop,
};
struct KfcRestartConfig {
    uint32_t tryRestartTimes{0U};
    uint32_t maxRestartTimes{1U}; // 最多重执行一次
};
const uint64_t kCreditTimeInvalid = 255U;
const uint64_t kCreditTimeDefault = 240U;  // 240对应到rts的credit字段就代表960s, 硬件同步时间
const uint64_t kTimeOutTimeInvalid = 0U;
const uint64_t kKfcTimeOut = 960U;         // 960s
const uint64_t kSqFullWaitTimeOut = 60U;   // 60s, rtsq full等待时间要大于超时代答时间
const uint64_t kPrintSqInterval = 30U;      // 算子执行阶段打印sqe状态的间隔，单位s

struct DfxTimeOutConfig {
    uint64_t sqeTimeOutTimeOut; // 软件同步
    uint64_t sqeCreditTimeOut; // 硬件同步
    bool useCredit; // 是否使用硬件同步
    uint64_t sqeWaitTimeOut; // kfc自己的超时
    uint64_t sqFullWaitTimeOut; // sq满的时候等待的超时
    std::string ToString() const
    {
        std::stringstream ss;
        ss << "sqeTimeOutTimeOut: " << sqeTimeOutTimeOut << " s";
        ss << " sqeCreditTimeOut: " << sqeCreditTimeOut << " s";
        ss << " useCredit: " << useCredit;
        ss << " sqeWaitTimeOut: " << sqeWaitTimeOut << " s";
        ss << " sqFullWaitTimeOut: " << sqFullWaitTimeOut << " s";
        return ss.str();
    }
};

struct TaskExceptionCqe {
    uint8_t sqeType{0};
    uint32_t errorCode{0};
};

struct DfxExtendInfo {
    KfcStatus kfcStatus = KfcStatus::kDefault;
    CqeStatus cqeStatus = CqeStatus::kDefault;
    PollStatus pollStatus = PollStatus::kDefault;
    TaskExceptionCqe cqeException;
    CommandToKfc commandToKfc = CommandToKfc::kDefault;
    KfcRestartConfig kfcRestartConfig;
    CommandToBackGroud commandToBackGroud = CommandToBackGroud::kDefault;
    DfxTimeOutConfig dfxTimeOutConfig;
};
struct CqeQueryInput {
    uint32_t devId;
    uint16_t streamId;
    uint32_t sqId;
    uint32_t cqId;
    uint32_t type;
    uint8_t *cqeAddr;
    std::string ToString() const
    {
        std::stringstream ss;
        ss << "devId: " << devId;
        ss << " streamId: " << streamId;
        ss << " sqId: " << sqId;
        ss << " cqId: " << cqId;
        ss << " type: " << type;
        return ss.str();
    }
};

class DfxExtendInfoHelper {
public:
    static void ResetTryRestartTimes(DfxExtendInfo &dfxExtendInfo);
    static void TryRestartOnceMore(DfxExtendInfo &dfxExtendInfo);
    static bool TryRestartTooManyTimes(const DfxExtendInfo &dfxExtendInfo);
};

}
#endif // ASCEND_ACE_COMOP_HCCL_HCCL_AI_CPU_KERNEL_DFX_DFX_EXTEND_INFO_H_
